/**
 * Rubik is a for-testing-purposes simple CMS.
 * Copyright (C) 2010 Antonio Hernandez Diaz <ahdiaz@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * $Id: Test_Setup.java 8 2010-04-13 19:10:09Z ahdiaz $
 */

package net.ahdiaz.rubik.tests;

import java.util.Collection;
import java.util.Iterator;

import javax.xml.bind.JAXBException;

import net.ahdiaz.rubik.model.RolesCollection;
import net.ahdiaz.rubik.model.TasksCollection;
import net.ahdiaz.rubik.model.UsersCollection;
import net.ahdiaz.rubik.model.generated.Contact;
import net.ahdiaz.rubik.model.generated.Role;
import net.ahdiaz.rubik.model.generated.RoleTask;
import net.ahdiaz.rubik.model.generated.RoleTasks;
import net.ahdiaz.rubik.model.generated.Task;
import net.ahdiaz.rubik.model.generated.Tlf;
import net.ahdiaz.rubik.model.generated.User;
import net.ahdiaz.rubik.model.generated.UserRole;
import net.ahdiaz.rubik.model.generated.UserRoles;
import ru.ispras.sedna.driver.DriverException;

/**
 * Clean up the database and create default content
 * using the JAXB generated classes.
 */
public class Test_Setup {

	public static void main(String[] args) {
		new Test_Setup();
	}
	
	public Test_Setup() {
		try {
			
			this.createTasks();
			this.createRoles();
			this.createUsers();
			
		} catch (DriverException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void createUsers() throws DriverException, JAXBException {
		
		UsersCollection users = new UsersCollection();
		User user = new User();
		
		Collection<User> c = users.readAll();
		Iterator<User> it = c.iterator();
		while (it.hasNext()) {
			users.remove(it.next());
		}
		
		user.setUid("admin");
		user.setPwd("admin");
		user.setFirst("annntonnnio");
		user.setLast("Hernandez");
		user.setEmail("ahernandez@rubiktest.net");		
		user.setContact(new Contact());
		Tlf tlf1 = new Tlf();
		tlf1.setType("mobile");
		tlf1.setValue("123456789");
		user.getContact().getTlf().add(tlf1);		
		user.setUserRoles(new UserRoles());
		UserRole ur1 = new UserRole();
		ur1.setUid("admin");
		user.getUserRoles().getUserRole().add(ur1);
		
		users.append(user);
		user.setFirst("Antonio");
		users.update(user);
		
		user.setUid("mgarcia");
		user.setPwd("mgarcia");
		user.setFirst("M.");
		user.setLast("garcia");
		user.setEmail("mgarcia@rubiktest.net");		
		user.setContact(new Contact());
		user.getContact().getTlf().add(tlf1);		
		user.setUserRoles(new UserRoles());
		ur1.setUid("redactor");
		user.getUserRoles().getUserRole().add(ur1);		
		users.append(user);
		
		user.setUid("dcela");
		user.setPwd("dcela");
		user.setFirst("D.");
		user.setLast("Cela");
		user.setEmail("dcela@rubiktest.net");		
		user.setContact(new Contact());
		user.getContact().getTlf().add(tlf1);		
		user.setUserRoles(new UserRoles());
		ur1.setUid("redactor");
		user.getUserRoles().getUserRole().add(ur1);		
		users.append(user);
		
		user.setUid("mlopez");
		user.setPwd("mlopez");
		user.setFirst("M.");
		user.setLast("Lopez");
		user.setEmail("mlopez@rubiktest.net");		
		user.setContact(new Contact());
		user.getContact().getTlf().add(tlf1);		
		user.setUserRoles(new UserRoles());
		ur1.setUid("editor");
		user.getUserRoles().getUserRole().add(ur1);		
		users.append(user);
		
	}
	
	private void createRoles() throws DriverException, JAXBException {
		
		RolesCollection roles = new RolesCollection();
		Role role = new Role();
		RoleTask rtask = new RoleTask();
		
		Collection<Role> c = roles.readAll();
		Iterator<Role> it = c.iterator();
		while (it.hasNext()) {
			roles.remove(it.next());
		}
		
		role.setUid("admin");
		role.setName("Administrator");
		role.setDescription("");
		role.setRoleTasks(new RoleTasks());
		rtask.setUid("admin");
		role.getRoleTasks().getRoleTask().add(rtask);
		roles.append(role);
		
		role.setUid("redactor");
		role.setName("Redactor");
		role.setDescription("");
		role.setRoleTasks(new RoleTasks());
		rtask.setUid("redact");
		role.getRoleTasks().getRoleTask().add(rtask);
		roles.append(role);
		
		role.setUid("editor");
		role.setName("Editor");
		role.setDescription("");
		role.setRoleTasks(new RoleTasks());
		rtask.setUid("publish");
		role.getRoleTasks().getRoleTask().add(rtask);
		roles.append(role);
		
	}
	
	private void createTasks() throws DriverException, JAXBException {
		
		TasksCollection tasks = new TasksCollection();
		Task task = new Task();
		
		Collection<Task> c = tasks.readAll();
		Iterator<Task> it = c.iterator();
		while (it.hasNext()) {
			tasks.remove(it.next());
		}
		
		task.setUid("admin");
		task.setDescription("");
		tasks.append(task);
		
		task.setUid("redact");
		task.setDescription("");
		tasks.append(task);
		
		task.setUid("publish");
		task.setDescription("");
		tasks.append(task);		
	}

}
